package controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URLClassLoader;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import models.LoanApp;
import play.Configuration;
import play.Play;
import play.db.jpa.JPAPlugin;
import play.mvc.Controller;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import play.mvc.Result;
import prefix.util.IOUtil;
import views.html.home;
import views.html.index;
import controllers.account.routes;

public class Application extends Controller {

    public static Result index() {

        return ok(index.render("cat"));
    }

    public static Result home() {
        return ok(home.render());
    }

    public static Result upload() {
        try {
            
//                        System.out.println(Thread.currentThread().getContextClassLoader());
            //            EntityManagerFactory factory = Persistence.createEntityManagerFactory("P2PDEVUnit");
            //            EntityManager em = factory.createEntityManager();
            //            System.out.println("application: " + Application.class.getClassLoader().toString().substring(0, 50));
            //            //            System.out.println(LoanApp.class.getClassLoader().toString());
            //            System.out.println("play : " + Play.application().classloader().toString().substring(0, 50));
            //            System.out.println(EntityManagerFactory.class.getClassLoader());

            //            classloaderer(Application.class);
            //            classloaderer(Play.class);
            //            classloaderer(LoanApp.class);

            LoanApp app = (LoanApp) Thread.currentThread().getContextClassLoader().loadClass("Models.LoanApp")
                    .newInstance();
            
        } catch (Throwable t) {
            t.printStackTrace();
        }

        return ok(views.html.upload.render());
    }

    private static void classloaderer(Class clz) {
        System.out.println(clz.toString() + " "
                + clz.getClassLoader().getParent().getParent().toString().substring(0, 50));
        System.out.println(clz.toString() + " "
                + clz.getClassLoader().getParent().getParent().toString().substring(0, 50));
    }

    public static Result submitUpload() {
        InputStream is = null;
        try {
            MultipartFormData body = request().body().asMultipartFormData();
            FilePart picture = body.getFile("picture");
            if (picture != null) {
                String fileName = picture.getFilename();
                String contentType = picture.getContentType();
                File file = picture.getFile();
                is = new FileInputStream(file);

                return ok(file.getName());
            } else {
                flash("error", "Missing file");
                return redirect(routes.Summary.index());
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            flash("error", "Missing file");
            return redirect(routes.Summary.index());
        } finally {
            IOUtil.closeQuietly(is);
        }
    }

}
